﻿using System;
using System.Collections.Generic;
using Xc.DataAccess.DataBase.DeviceInfos.Models;
using Microsoft.EntityFrameworkCore;
using Xc.DataAccess.Core;
using System.Threading.Tasks;
using System.Linq;
namespace Xc.DataAccess.DataBase.DeviceInfos
{
    /// <summary>
    /// 历史表信息服务
    /// </summary>
    /// <typeparam name="TDbContext"></typeparam>
    public abstract class TableInfoService<TDbContext, TInfo, TInfoHistory> : TableInfoServiceCore<TDbContext>, ITableInfoService<TInfo,TInfoHistory> where TDbContext : DbContext, IDbDevice where TInfoHistory : ITableInfo,IDateTimeCreate
    {
        public TableInfoService(IDbContextFactory<TDbContext> dbContextFactory) : base(dbContextFactory)
        {

        }
        /// <summary>
        /// 获取最新属性
        /// </summary>
        /// <param name="deviceid"></param>
        /// <returns></returns>
        public abstract ResultData<TInfo[]> Gets(string deviceid);
        /// <summary>
        /// 获取历史属性
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="dateTimeStart"></param>
        /// <param name="dateTimeEnd"></param>
        /// <returns></returns>
        public abstract Task<TInfoHistory[]> Gets(string deviceId, DateTime? dateTimeStart, DateTime? dateTimeEnd);
        /// <summary>
        /// 获取属性表
        /// </summary>
        /// <param name="deviceId">设备编号</param>
        /// <param name="autoAppendLastState">自动补齐最新状态</param>
        /// <param name="dateTimeStart">起始时间</param>
        /// <param name="dateTimeEnd">结束时间</param>
        /// <returns></returns>
        public async Task<DataTableInfo> GetTable(string deviceId, bool autoAppendLastState, DateTime? dateTimeStart, DateTime? dateTimeEnd)
        {
            var datas = await Gets(deviceId, dateTimeStart, dateTimeEnd);
            return GetTableInfo(datas, autoAppendLastState);
        }
        /// <summary>
        /// 删除指定信息
        /// </summary>
        /// <param name="deviceId"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public abstract  Task<Result> Remove(string deviceId, string name);
        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="valuePairs"></param>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public abstract Task<Result> Set(Dictionary<string, UnitString> valuePairs, string deviceId);
        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <param name="deviceId"></param>
        /// <returns></returns>
        public Task<Result> Set(string key, UnitString value, string deviceId)
        {
            return Set(new Dictionary<string, UnitString> { { key, value } }, deviceId);
        }
    }

}
